<?php


namespace app\crontab\controller\once;


use custom\Log;
use custom\Mongo;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

/**
 * 补救房源的小区id
 * Class RemedyHouseVid
 * @package app\crontab\controller
 */
class RemedyHouseVid extends Command {

    protected function configure() {
        $this->setName('RemedyHouseVid');
    }

    protected function execute(Input $input, Output $output) {
        echo "start\n";
        $this->foo();
        echo "end\n";
    }


    protected function test() {
        $err = [7720, 2120, 2292, 8344, 5913, 745, 8659, 8164, 8239, 195, 2543, 2508, 3108, 2497, 1125, 8393, 8609, 71, 8554, 8556, 8951];
        $list = db('house')
            ->alias('h')
            ->leftJoin('house_village hv', 'hv.id = h.village_id')
            ->where('h.id', 'in', $err)
            ->field('h.id as house_id,h.source_city,hv.name,h.title,hv.city')
            ->select();
        Log::write($list, 'Testing.log');
    }

    protected function foo() {
        $errMsg = [];
        $save = [];
        $list = db('house')
            ->where('village_id', '>', 0)
            ->field('id,source_city,city,village_id')
            ->select();
        foreach ($list as $index => $item) {
            $a = str_replace('市', '', $item['source_city']);
            $b = str_replace('市', '', $item['city']);
            echo $item['id'] . "\n";
            if ($a != $b) {
                $v_name = db('house_village')
                    ->where('id', $item['village_id'])
                    ->value('name');
                $cy = str_replace('市', '', $item['source_city']);
                $vData = db('house_village')
                    ->where('city', 'like', $cy . '%')
                    ->where('name', $v_name)
                    ->find();
                if (empty($vData)) {
                    $errMsg[] = $item['id'];
                    continue;
                } else {
                    $data['village_id'] = $vData['id'];
                    $data['business_area_id'] = $vData['business_area'];
                    $data['address'] = $vData['address'];
                    $data['province'] = $vData['province'];
                    $data['city'] = $vData['city'];
                    $data['area'] = $vData['area'];
                    $data['lng'] = $vData['lng'];
                    $data['lat'] = $vData['lat'];
                    $data['location'] = Db::raw("POINT('{$vData['lng']}', '{$vData['lat']}')");
                    $data['city_code'] = $vData['city_code'];
                    $data['city_codes'] = $vData['city_codes'];
                    if (db('house')
                            ->where('id', $item['id'])
                            ->update($data) !== false) {
                        $save[] = $item['id'];
                    }
                }
            }
        }
        Log::write('total->' . count($list) . "\n" . 'err->' . json_encode($errMsg) . "\n" . 'suc->' . json_encode($save) . "\n", 'RemedyHouseVid.log');
    }

}